import os
import yaml


# 获取根路径
def root_path():
    """ 获取 根路径 """
    path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    return path


# 写入yaml文件
def write_yaml(data):
    with open(root_path() + r'\common\config.yaml', encoding='utf-8', mode="a+") as f:
        yaml.dump(data, stream=f, allow_unicode=True)


# 读取yaml文件
def read_yaml(key):
    with open(root_path() + r'\common\config.yaml', encoding='utf-8', mode="r") as f:
        value = yaml.load(f, yaml.FullLoader)
        return value[key]


# 清空yaml文件
def clear_yaml():
    with open(root_path() + r'\common\config.yaml', encoding='utf-8', mode="w") as f:
        f.truncate()


# 读取token
def clear_yaml_token(access_token):
    with open(root_path() + r'\common\config.yaml', encoding='utf-8', mode="w") as f:
        dict_temp = yaml.load(f, Loader=yaml.FullLoader)
        dict_temp.pop(access_token)


# 读取用例
def read_yaml_case(yamlpath):
    with open(root_path() + '/' + yamlpath, encoding='utf-8', mode="r") as f:
        value = yaml.load(f, yaml.FullLoader)
        return value
